package com.minshenglife.zanzan.web.weixin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.entity.EndPlateReview;
import com.minshenglife.zanzan.pub.utils.Message;
import com.minshenglife.zanzan.result.EndPlateResult;
import com.minshenglife.zanzan.result.EndPlateReviewResult;
import com.minshenglife.zanzan.service.EndPlateReviewService;
import com.minshenglife.zanzan.service.EndPlateService;
import com.minshenglife.zanzan.web.weixin.form.EPReviewForm;
import com.minshenglife.zanzan.web.weixin.form.StatForm;
import com.nationsky.pub.utils.StringUtils;


/**
 * 
 * @author huodd
 * 
 */
@Controller
@RequestMapping("/endplateReview")
public class EndPlateReviewController extends BaseController {
	Logger log = LoggerFactory.getLogger(EndPlateReviewController.class);
	@Resource
	EndPlateReviewService endPlateReviewService;
	@Resource
	EndPlateService endPlateService;
    
	/**
     * 更新吐槽被赞的数量
     * @param request
     * @param response
     * @param reviewForm
     * @return
     */
    @RequestMapping(value="/updatePraiseNum.do",method=RequestMethod.POST)
    @ResponseBody
    public Message updatePraiseNum(HttpServletRequest request, HttpServletResponse response,
    		EPReviewForm reviewForm){
    	Message m=null;
    	try {
    		String id=reviewForm.getToEndPlateId();
    		int num=Integer.parseInt(reviewForm.getPraiseNum());
    		Long.parseLong(id);
    		if(StringUtils.isNullOrEmpty(id)){
    			m=new Message(Message.STATE_one,"标识符异常");
    		}else{
    			num=endPlateService.updateEndPlatePraiseNum(id, num);
    			log.debug("更新被赞的数量是："+num);
    			m=new Message(Message.STATE_200,""+num);
    		}
    	} catch (Exception e) {
			e.printStackTrace();
			m=new Message(Message.STATE_one,"服务器异常");
			log.debug(m.toString());
		}
    	return m;
    }
	/**
     * 根据id获取吐槽的详细信息
     * @param request
     * @param response
     * @param reviewForm
     * @return
     */
    @RequestMapping(value="/getEndPlateInfoById.do",method=RequestMethod.POST)
    @ResponseBody
    public Message getEndPlateInfoById(HttpServletRequest request, HttpServletResponse response,
    		EPReviewForm reviewForm){
    	Message m=null;
    	try {
    		String id=reviewForm.getToEndPlateId();
    		if(StringUtils.isNullOrEmpty(id)){
    			m=new Message(Message.STATE_one,"标识符异常");
    		}else{
    			EndPlateResult result=endPlateService.getEndPlateInfoById(id);
    			m=new Message(Message.STATE_200,"",result);
    		}
    	} catch (Exception e) {
			e.printStackTrace();
			m=new Message(Message.STATE_one,"服务器异常");
			log.debug(m.toString());
		}
    	return m;
    }
    		
    /**
     * 添加吐槽的评论
     * @param request
     * @param response
     * @param reviewForm
     * @return
     */
    @RequestMapping(value="/addEPReview.do",method=RequestMethod.POST)
    @ResponseBody
    public Message addEPReview(HttpServletRequest request, HttpServletResponse response,
    		EPReviewForm reviewForm){
    	Message m=new Message(Message.STATE_200,"");
    	try {
    		String operator=super.getEmpId(request);
			log.debug("当前操作人："+operator);
			log.debug("被评论的吐槽："+reviewForm.getToEndPlateId());
			log.debug("评论人类型："+reviewForm.getCriticsType());
			log.debug("被评论内容："+reviewForm.getContent());
			String content=reviewForm.getContent();
			//校验评论的内容
			if(StringUtils.isNullOrEmpty(content)){
				m=new Message(Message.STATE_one,"评论内容不能为空！");
			}else if(content.getBytes("UTF-8").length>80){
				m=new Message(Message.STATE_one,"评论内容字符过多，请确认！");
			}else{
				EndPlateReview epr=new EndPlateReview();
				epr.setEndPlateId(Long.parseLong(reviewForm.getToEndPlateId()));
				epr.setEmpId(operator);
				epr.setEprCon(content);
				epr.setEprType(reviewForm.getCriticsType());
	    		endPlateReviewService.add(epr);
			}
		} catch (Exception e) {
			e.printStackTrace();
			m=new Message(Message.STATE_one,"服务器异常");
			log.debug(m.toString());
		}
    	return m;
    }
    
    /**
     * 吐槽评论列表
     * @param request
     * @param response
     * @param statForm
     * @return
     */
    @RequestMapping(value="/EPReviewList.do",method=RequestMethod.POST)
    @ResponseBody
    public Message EPReviewList(HttpServletRequest request, HttpServletResponse response,
    		StatForm statForm){
    	Message message=null;
    	Page<EndPlateReviewResult> page;
		try {
			String id=statForm.getEndPlateId();
    		if(StringUtils.isNullOrEmpty(id)){
    			message=new Message(Message.STATE_one,"标识符异常");
    		}else{
				int pageNo=statForm.getPageNo()==0? 1: statForm.getPageNo();
				int pageSize=statForm.getPageSize()==0? 10: statForm.getPageSize();
				page = endPlateReviewService.findEndPlateInfo(statForm.getEndPlateId(), pageNo, pageSize);
				message=new Message(Message.STATE_200,"",page);
    		}
		} catch (Exception e) {
			e.printStackTrace();
			message=new Message(Message.STATE_one,"服务器异常");
			log.debug(message.toString());
		}
    	 return message;
    }
    
    
}
